package com.example.base_kotlin_xml.view

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.LogUtils
import com.example.base_kotlin_xml.R
import com.example.base_kotlin_xml.databinding.ActivityTwoBinding
import com.example.base_model.general.GlobalStateManager
import com.example.base_model.utils.FlowEventBus
import com.example.base_model.view.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class TwoActivity : BaseActivity<ActivityTwoBinding>() {
    @Inject
    lateinit var globalState: GlobalStateManager

    override fun initObservers() {
        FlowEventBus.observeEvent<String>(lifecycleScope, "123"){
            LogUtils.d("TwoActivity 接收到事件", it)
        }
        lifecycleScope.launch {
            globalState.appTitle.state.collect {
                LogUtils.d("TwoActivity appTitle 变化了: ", it) }
        }
        launchAndCollect(Lifecycle.State.STARTED) {
        }
    }

    override fun initClickEvents() {
        mBinding.btnCeshi.setOnClickListener {
            FlowEventBus.postEvent("123","666") }
        mBinding.btnLogin.setOnClickListener {
            FlowEventBus.postEvent("123","第二条数据发送") }
        mBinding.btnExit.setOnClickListener {
            FlowEventBus.postEvent("123","第三条多数据测试")
            FlowEventBus.postEvent("123","第三条多数据测试")
            FlowEventBus.postEvent("123","第三条多数据测试") }
        mBinding.btnChangeStateOne.setOnClickListener {
            globalState.appTitle.update("测试修改标题 一")
        }
        mBinding.btnChangeStateTwo.setOnClickListener {
            globalState.appTitle.update("测试修改标题 二")
        }
        mBinding.btnChangeStateThree.setOnClickListener {
            globalState.appTitle.update("测试修改标题 三")
        }
    }

    override fun initData() {

    }

    override fun attachLayoutRes(): Int = R.layout.activity_two
}